//
//  SNTrackCell.m
//  project
//
//  Created by 圆圆陈 on 2017/12/8.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNTrackCell.h"
#import "UIImageView+WebCache.h"
@implementation SNTrackCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.mapImageView = [[UIImageView alloc]initWithFrame:CGRectMake(FitW(10), FitH(15), FitW(85), FitW(85))];
        self.mapImageView.layer.cornerRadius = FitW(10);
        self.mapImageView.clipsToBounds = YES;
        [self.contentView addSubview:self.mapImageView];
        
        CGFloat width = (HHHWIDTH - self.mapImageView.right-FitW(10))/3;
        self.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.mapImageView.right+FitW(10), FitH(20),width*2, FitH(20))];
        self.addressLabel.textColor = UIColorHEX(0x000000);
        self.addressLabel.font = [UIFont systemFontOfSize:FitFont(20)];
        self.addressLabel.adjustsFontSizeToFitWidth = YES;
        self.addressLabel.text = @"西藏省-家里蹲市-胡来区";
        [self.contentView addSubview:self.addressLabel];
        
        self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.addressLabel.right, FitH(20),width, FitH(20))];
        self.timeLabel.textColor = UIColorHEX(0x727272);
        self.timeLabel.font = [UIFont systemFontOfSize:FitFont(12)];
        self.timeLabel.adjustsFontSizeToFitWidth = YES;
        self.timeLabel.text = @"2017/10/9 14:22";
        [self.contentView addSubview:self.timeLabel];
       
        NSArray *titleArr = @[HHH_Text(@"总里程"),HHH_Text(@"消耗大卡"),HHH_Text(@"总耗时")];
        for (int i=0; i<3; i++) {
            if (i==0) {
                self.disLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.mapImageView.right+FitW(10)+width*i, self.timeLabel.bottom+FitH(8), width, FitH(25))];
                self.disLabel.textColor = UIColorHEX(0x000000);
                self.disLabel.textAlignment = NSTextAlignmentCenter;
                self.disLabel.font =SNFONT(FitFont(20));
                self.disLabel.attributedText = [self changeLabelWithText:@"23.55km"];
                [self.contentView addSubview:self.disLabel];
            }else if (i==1){
                self.calLabel =[[UILabel alloc]initWithFrame:CGRectMake(self.mapImageView.right+FitW(10)+width*i, self.timeLabel.bottom+FitH(8), width, FitH(25))];
                self.calLabel.textColor = UIColorHEX(0x000000);
                self.calLabel.textAlignment = NSTextAlignmentCenter;
                self.calLabel.text = @"1455";
                self.calLabel.font =SNFONT(FitFont(20));
                [self.contentView addSubview:self.calLabel];
            }else{
                self.perLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.mapImageView.right+FitW(10)+width*i, self.timeLabel.bottom+FitH(8), width, FitH(24))];
                self.perLabel.textColor = UIColorHEX(0x000000);
                self.perLabel.textAlignment = NSTextAlignmentCenter;
                self.perLabel.font =SNFONT(FitFont(20));
                self.perLabel.attributedText = [self changeLabelWithText:@"14h32m"];
                [self.contentView addSubview:self.perLabel];
            }
            UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(self.mapImageView.right+FitW(10)+width*i, self.disLabel.bottom+FitH(5), width, FitH(20))];
            label.text = titleArr[i];
            label.textColor =UIColorHEX(0x727272);
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:FitFont(15)];
            [self.contentView addSubview:label];
            
        }
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.mapImageView.bottom+FitH(15), HHHWIDTH, 2)];
        line.backgroundColor = UIColorRGB(216, 216, 216);
        [self.contentView addSubview:line];
        self.cellH+=line.bottom;
    }
    return self;
}

-(NSMutableAttributedString*) changeLabelWithText:(NSString*)string
{
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    UIFont *font = SNFONT(FitFont(16));
    for (int i = 0; i <string.length; i++) {
        unichar ch = [string characterAtIndex:i];
        NSRange range = {i, 1};
        if (!('0' <= ch && ch <= '9')) {
            [attrString addAttribute:NSFontAttributeName value:font range:range];
        }else{
            [attrString addAttribute:NSFontAttributeName value:SNFONT(FitFont(20)) range:range];
        }
    }
    return attrString;
}
-(void)setInfoWithModel:(TrackModel *)model{
//    NSString *path_document = NSHomeDirectory();
//    NSString *imagePath = [path_document stringByAppendingString:model.imagePath];
//    self.mapImageView.image = [UIImage imageWithPath:imagePath];
    [self.mapImageView sd_setImageWithURL:[NSURL URLWithString:model.imagePath]];
    self.addressLabel.text = model.address;
    NSString *timeStr = [model.startTime stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    self.timeLabel.text = [timeStr substringToIndex:timeStr.length-3];
    NSString *disStr = SN_USER.isMetricSystem?[NSString stringWithFormat:@"%.2fmile",roundf(model.totalDistances*0.0006214*100)/100]:[NSString stringWithFormat:@"%.2fkm",model.totalDistances*1.0/1000];
    self.disLabel.attributedText = [self changeLabelWithText:disStr];
    NSString *calStr = [NSString stringWithFormat:@"%.2fkcal",SN_USER.weight*model.totalDistances*1.0/1000*1.036];
    self.calLabel.attributedText = [self changeLabelWithText:calStr];
    NSString *perStr =[NSString stringWithFormat:@"%ldh%ldm",model.sportTotalTime/3600,(model.sportTotalTime/60)%60];
    self.perLabel.attributedText = [self changeLabelWithText:perStr];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
